package com.rest.demo.nio.execrise;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-11 11:12
 *
 * <li>缓冲区三点:position limit capacity</li>
 * <li>缓冲区分片slice</li>
 * <li>缓冲区可设置为只读,但是只读缓冲不能改为可写,asReadOnlyBuffer</li>
 */
public class FileOutputChannelDemo {

    private static final byte MESSAGE[] = { 83, 111, 109, 101, 32,
            98, 121, 116, 101, 115, 46 };

    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream(new File("F:\\numOut.txt"));
        FileChannel fileChannel = fos.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);

        for (int i = 0; i < byteBuffer.capacity(); i++) {
            byteBuffer.put(MESSAGE[i]);
        }

        byteBuffer.flip();

        fileChannel.write(byteBuffer);
        fileChannel.close();
    }
}
